<?php

namespace App\Admin\Controllers;

use App\Enums\AccountAction;
use App\Models\Account;
use App\Models\Withdraw;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class WithdrawController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(Withdraw::with(['user']), function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('user_id');
            $grid->column('user.email', '邮箱');
            $grid->column('user.mobile', '手机');
            $grid->column('hash');
            $grid->column('number');
            $grid->column('fee');
            $grid->column('address');
            $statusArray = [
                0 => '未处理',
                1 => '已到账',
                2 => '已取消',
            ];
            $grid->column('status')->using($statusArray);
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) use ($statusArray) {
                $filter->equal('id');
                $filter->equal('user_id');
                $filter->equal('user.email', '邮箱');
                $filter->equal('user.mobile', '手机');
                $filter->equal('address');
                $filter->equal('hash');
                $filter->equal('status')->select($statusArray);
            });
            $grid->disableCreateButton();
            $grid->disableBatchActions();
            $grid->disableRowSelector();
            $grid->disableViewButton();
            $grid->disableDeleteButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(Withdraw::with(['user']), function (Form $form) {
            $form->display('id');
            $form->display('user.email', '邮箱');
            $form->display('user.mobile', '手机号');
            $form->display('user_id');
            $form->text('hash');
            $form->display('number');
            $form->display('fee');
            $form->display('address');
            $form->radio('status', '操作')->options([0 => '暂不处理', 1 => '通过', 2 => '撤销']);

            DB::transaction(function () use ($form) {
                if($form->model()->status != 0){
                    return $form->response()->error('已处理过');
                }
                /** @var Account $account */
                $account = Account::where('user_id', $form->model()->user_id)->where('coin', 'CK')->lockForUpdate()->first();
                if ($form->status == 2) {
                    $account->lock2free($form->model()->number, AccountAction::WithdrawCancelled, $form->model());
                }elseif($form->status == 1){
                    $account->lockDecr($form->model()->number, AccountAction::WithdrawPass, $form->model());
                }else{

                }
            });

            $form->footer(function ($footer) {
                // 去掉`查看`checkbox
                $footer->disableViewCheck();
                // 去掉`继续编辑`checkbox
                $footer->disableEditingCheck();
                // 去掉`继续创建`checkbox
                $footer->disableCreatingCheck();
            });
            $form->disableViewButton();
            $form->disableDeleteButton();
        });
    }
}
